package com.pbs.ui;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class MySharedPreferences{
	
	private static MySharedPreferences my_preferences=null;
	private static SharedPreferences preferences=null;
	private static Editor editor = null;
	
	private static final String	STRING_NAME_USERSETTING	= "USER REFERENCES";
	
	private MySharedPreferences(Context context) {
		preferences = context.getSharedPreferences(STRING_NAME_USERSETTING, Activity.MODE_PRIVATE);
		editor = preferences.edit();
	}
	
	public static MySharedPreferences instance(Context context){
		if(my_preferences==null){
			my_preferences = new MySharedPreferences(context);
		}
		return my_preferences;
	}
	
	public void addSharedPreferences(String key, String value) throws Exception{
		if(key.trim().equals("")){
			throw new Exception("Key value is empty!");
		}
		if (value.trim().equals("")) {
			editor.remove(key);
		} else {
			editor.putString(key, value);
		}
		editor.commit();
	}
	
	public void addSharedPreferences(String key, boolean value) throws Exception{
		if(key.trim().equals("")){
			throw new Exception("Key value is empty!");
		}
		editor.putBoolean(key, value);
		editor.commit();
	}

	public String getSharedPreferences(String key, String defValue) {
		return preferences.getString(key, defValue);
	}
	public boolean getSharedPreferences(String key, boolean defValue) {
		return preferences.getBoolean(key, defValue);
	}
}
